// // Copyright (c) 2009 All Right Reserved // // vl // // 2009-01-01 // Contains ... using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Text; using JetBrains.Annotations; using LargoCommon.Support; namespace LargoCommon.Music { /// /// Orchestra Strip. /// public class OrchestraStrip { #region Fields /// Unique Identifier. private string uniqueIdentifier; #endregion #region Constructors /// /// Initializes a new instance of the OrchestraStrip class. /// public OrchestraStrip() { this.OrchestraTracks = new List(); } #endregion #region Public properties /// /// Gets or sets the musical voice. /// /// /// The musical voice. /// public IList OrchestraTracks { get; set; } /// /// Gets the melodic orchestra tracks. /// /// /// The melodic orchestra tracks. /// public IList MelodicOrchestraTracks => (from t in this.OrchestraTracks where t.Instrument.Genus == InstrumentGenus.Melodical select t).ToList(); /// /// Gets the rhythmic orchestra tracks. /// /// /// The rhythmic orchestra tracks. /// public IList RhythmicOrchestraTracks => (from t in this.OrchestraTracks where t.Instrument.Genus == InstrumentGenus.Rhythmical select t).ToList(); /// Gets Unique Identifier. /// Property description. [UsedImplicitly] public string UniqueIdentifier { get { if (this.uniqueIdentifier != null) { return this.uniqueIdentifier; } var ident = new StringBuilder(); var tracks = (from mv in this.OrchestraTracks orderby mv.Octave, mv.Instrument select mv).Distinct(); foreach (var track in tracks) { ident.Append(track.UniqueIdentifier); } this.uniqueIdentifier = ident.ToString(); return this.uniqueIdentifier; } } /// /// Gets or sets the instrument count. /// /// /// The instrument count. /// public int InstrumentCount { get; set; } /// /// Gets or sets the section count. /// /// /// The section count. /// public int SectionCount { get; set; } #endregion #region Public methods /// /// THe optimal orchestra track. /// /// The musical track. /// /// Returns value. /// public OrchestraTrack OptimalOrchestraTrack(MusicalLine mtrack) { Contract.Requires(mtrack != null); if (!mtrack.FirstStatus.IsMelodic) { return null; } const byte fullness = 20; //// (30) const byte momentum = 20; //// (10) var bestValue = 0; OrchestraTrack bestTrack = null; //// int randomPartNumber = MathSupport.RandomNatural(voices.Count()); var trackOctave = mtrack.Tones.MeanOctave; //// mtrack.MusicalOctave foreach (var v in this.MelodicOrchestraTracks) { var value = 100 - (10 * Math.Abs((byte)v.Octave - (byte)trackOctave)); if (!v.IsUsed) { value += fullness; } if (mtrack.FirstStatus.CurrentOrchestraTrack != null && mtrack.FirstStatus.CurrentOrchestraTrack.InstrumentNumber == v.InstrumentNumber) { value += momentum; } //// value += v.PartNumber % 3; //// MathSupport.RandomNatural(3); if (value <= bestValue) { continue; } bestValue = value; bestTrack = v; } return bestTrack; } /// /// Recompute the properties. /// public void RecomputeProperties() { foreach (var track in this.OrchestraTracks) { if (track.Instrument == null) { continue; } if (track.Instrument.Genus == InstrumentGenus.Melodical) { track.Instrument.Section = (byte)InstrumentsPorter.GetGroupOfMelodicInstrument(track.Instrument.Number); } if (track.Instrument.Genus == InstrumentGenus.Rhythmical) { track.Instrument.Section = (byte)InstrumentsPorter.GetGroupOfRhythmicInstrument(track.Instrument.Number); } } var number = (from track in this.OrchestraTracks select track.InstrumentNumber).Distinct().Count(); this.InstrumentCount = number; number = (from track in this.OrchestraTracks where track.Instrument != null select track.Instrument.Section).Distinct().Count(); this.SectionCount = number; } #endregion #region String representation /// String representation of the object. /// Returns value. public override string ToString() { var s = new StringBuilder(); var tracks = (from mv in this.OrchestraTracks orderby mv.Octave, mv.Instrument select mv).Distinct(); foreach (var track in tracks) { s.Append("Octave:" + track.Octave); //// musicalVoice.MelodicInstrument s.Append("Instrument" + track.Instrument); } return s.ToString(); } #endregion } }